<template>
  <view class="register-container">
    <view class="register-header">
      <text class="back-btn" @click="goBack">←</text>
      <text class="register-title">注册</text>
    </view>
    
    <scroll-view class="form-scroll" scroll-y>
      <view class="register-form">
        <!-- 负责人信息 -->
        <view class="form-section">
          <view class="form-item">
            <text class="label">负责人</text>
            <input 
              class="input" 
              placeholder="输入负责人姓名"
              v-model="formData.contactPerson"
            />
          </view>
          
          <view class="form-item">
            <text class="label">身份证号码</text>
            <input 
              class="input" 
              placeholder="输入负责人身份证号码"
              v-model="formData.idCard"
            />
          </view>
          
          <view class="form-item">
            <text class="label">手机号</text>
            <input 
              class="input" 
              type="number" 
              placeholder="输入手机号"
              v-model="formData.phoneNumber"
              maxlength="11"
            />
          </view>
          
          <view class="form-item verification-item">
            <text class="label">验证码</text>
            <input 
              class="input verification-input" 
              type="number" 
              placeholder="输入验证码"
              v-model="formData.verificationCode"
              maxlength="6"
            />
            <button 
              class="verification-btn" 
              @click="getVerificationCode"
              :disabled="countdown > 0"
            >
              {{ countdown > 0 ? `${countdown}s后重新获取` : '获取验证码' }}
            </button>
          </view>
        </view>
        
        <!-- 店铺信息 -->
        <view class="form-section">
          <view class="form-item">
            <text class="label">店名</text>
            <input 
              class="input" 
              placeholder="输入店名"
              v-model="formData.storeName"
            />
          </view>
          
          <view class="form-item">
            <text class="label">工商营业执照号码</text>
            <input 
              class="input" 
              placeholder="输入营业执照号"
              v-model="formData.businessLicense"
            />
          </view>
          
          <view class="form-item">
            <text class="label">经营区域</text>
            <view class="picker-wrapper">
              <picker mode="region" @change="onRegionChange">
                <view class="picker-display">
                  {{ region.join(' ') || '选择区县' }}
                </view>
              </picker>
              <text class="picker-arrow">下</text>
            </view>
          </view>
          
          <view class="form-item">
            <text class="label">地址</text>
            <input 
              class="input" 
              placeholder="输入详细地址"
              v-model="formData.address"
            />
          </view>
          
          <view class="form-item">
            <text class="label">商家介绍</text>
            <textarea 
              class="textarea" 
              placeholder="输入商家介绍"
              v-model="formData.storeDescription"
              auto-height
              maxlength="200"
            ></textarea>
          </view>
        </view>
        
        <!-- 图片上传 -->
        <view class="form-section">
          <view class="form-item">
            <text class="label">身份证正反面照片</text>
            <view class="upload-container">
              <view class="upload-item" @click="uploadImage('idCardFront')">
                <image v-if="formData.idCardFront" :src="formData.idCardFront" class="uploaded-image"></image>
                <view v-else class="upload-placeholder">
                  <text class="upload-plus">+</text>
                  <text class="upload-text">上传照片</text>
                </view>
              </view>
            </view>
          </view>
          
          <view class="form-item">
            <text class="label">营业执照</text>
            <view class="upload-container">
              <view class="upload-item" @click="uploadImage('businessLicenseImg')">
                <image v-if="formData.businessLicenseImg" :src="formData.businessLicenseImg" class="uploaded-image"></image>
                <view v-else class="upload-placeholder">
                  <text class="upload-plus">+</text>
                  <text class="upload-text">上传照片</text>
                </view>
              </view>
            </view>
          </view>
          
          <view class="form-item">
            <text class="label">门面照片</text>
            <view class="upload-container">
              <view class="upload-item" @click="uploadImage('storeFront')">
                <image v-if="formData.storeFront" :src="formData.storeFront" class="uploaded-image"></image>
                <view v-else class="upload-placeholder">
                  <text class="upload-plus">+</text>
                  <text class="upload-text">上传照片</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
    
    <!-- 底部注册按钮 -->
    <view class="bottom-actions">
      <button 
        class="register-btn" 
        @click="register"
      >
        注册
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      countdown: 0,
      timer: null,
      region: [],
      formData: {
        contactPerson: '',
        idCard: '',
        phoneNumber: '',
        verificationCode: '',
        storeName: '',
        businessLicense: '',
        address: '',
        storeDescription: '',
        idCardFront: '',
        businessLicenseImg: '',
        storeFront: ''
      }
    };
  },
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },
    
    // 获取验证码
    getVerificationCode() {
      // 简单的手机号验证
      if (!/^1[3-9]\d{9}$/.test(this.formData.phoneNumber)) {
        uni.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
        return;
      }
      
      // 模拟发送验证码
      uni.showToast({
        title: '验证码已发送',
        icon: 'success'
      });
      
      // 开始倒计时
      this.countdown = 60;
      this.startCountdown();
    },
    
    // 倒计时函数
    startCountdown() {
      this.timer = setInterval(() => {
        if (this.countdown > 0) {
          this.countdown--;
        } else {
          clearInterval(this.timer);
        }
      }, 1000);
    },
    
    // 地区选择变化
    onRegionChange(e) {
      this.region = e.detail.value;
    },
    
    // 图片上传
    uploadImage(type) {
      uni.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          const tempFilePath = res.tempFilePaths[0];
          // 模拟上传，实际项目中需要调用上传接口
          this.formData[type] = tempFilePath;
          uni.showToast({
            title: '上传成功',
            icon: 'success'
          });
        },
        fail: () => {
          uni.showToast({
            title: '上传失败',
            icon: 'none'
          });
        }
      });
    },
    
    // 注册
    register() {
      // 表单验证
      if (!this.formData.contactPerson) {
        uni.showToast({ title: '请输入负责人姓名', icon: 'none' });
        return;
      }
      
      if (!this.formData.idCard) {
        uni.showToast({ title: '请输入身份证号码', icon: 'none' });
        return;
      }
      
      if (!/^1[3-9]\d{9}$/.test(this.formData.phoneNumber)) {
        uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
        return;
      }
      
      if (!this.formData.verificationCode) {
        uni.showToast({ title: '请输入验证码', icon: 'none' });
        return;
      }
      
      if (!this.formData.storeName) {
        uni.showToast({ title: '请输入店名', icon: 'none' });
        return;
      }
      
      if (!this.formData.businessLicense) {
        uni.showToast({ title: '请输入营业执照号码', icon: 'none' });
        return;
      }
      
      if (this.region.length === 0) {
        uni.showToast({ title: '请选择经营区域', icon: 'none' });
        return;
      }
      
      if (!this.formData.idCardFront) {
        uni.showToast({ title: '请上传身份证照片', icon: 'none' });
        return;
      }
      
      if (!this.formData.businessLicenseImg) {
        uni.showToast({ title: '请上传营业执照', icon: 'none' });
        return;
      }
      
      // 模拟注册成功
      uni.showToast({
        title: '注册成功',
        icon: 'success'
      });
      
      // 注册成功后返回登录页面
      setTimeout(() => {
        uni.navigateBack();
      }, 1500);
    }
  },
  
  // 页面卸载时清除定时器
  onUnload() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
};
</script>

<style lang="scss" scoped>
.register-container {
  padding: 0 40rpx;
  box-sizing: border-box;
  min-height: 100vh;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
}

.register-header {
  position: relative;
  height: 120rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1rpx solid #f0f0f0;
}

.back-btn {
  position: absolute;
  left: 0;
  font-size: 48rpx;
  color: #333333;
}

.register-title {
  font-size: 36rpx;
  color: #333333;
  font-weight: 600;
}

.form-scroll {
  flex: 1;
  margin-bottom: 20rpx;
}

.register-form {
  padding: 20rpx 0;
}

.form-section {
  margin-bottom: 40rpx;
}

.form-item {
  margin-bottom: 32rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #666666;
  margin-bottom: 16rpx;
}

.input {
  width: 100%;
  height: 96rpx;
  line-height: 96rpx;
  font-size: 32rpx;
  color: #333333;
  border-bottom: 1rpx solid #e5e5e5;
  padding: 0 20rpx;
  box-sizing: border-box;
}

.verification-item {
  position: relative;
}

.verification-input {
  width: 50%;
}

.verification-btn {
  position: absolute;
  right: 20rpx;
  top: 32rpx;
  height: 64rpx;
  line-height: 64rpx;
  font-size: 28rpx;
  color: #1989fa;
  background-color: transparent;
  border: none;
  padding: 0 20rpx;
}

.verification-btn[disabled] {
  color: #999999;
}

.picker-wrapper {
  position: relative;
  height: 96rpx;
  line-height: 96rpx;
  border-bottom: 1rpx solid #e5e5e5;
  padding: 0 20rpx;
}

.picker-display {
  font-size: 32rpx;
  color: #333333;
}

.picker-arrow {
  position: absolute;
  right: 20rpx;
  top: 0;
  font-size: 24rpx;
  color: #999999;
}

.textarea {
  width: 100%;
  min-height: 160rpx;
  font-size: 32rpx;
  color: #333333;
  border: 1rpx solid #e5e5e5;
  border-radius: 12rpx;
  padding: 20rpx;
  box-sizing: border-box;
  resize: none;
}

.upload-container {
  display: flex;
  gap: 20rpx;
}

.upload-item {
  width: 200rpx;
  height: 200rpx;
  border: 1rpx dashed #e5e5e5;
  border-radius: 12rpx;
  position: relative;
  overflow: hidden;
}

.upload-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.upload-plus {
  font-size: 64rpx;
  color: #cccccc;
  margin-bottom: 10rpx;
}

.upload-text {
  font-size: 24rpx;
  color: #999999;
}

.uploaded-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.bottom-actions {
  padding: 20rpx 0 40rpx;
}

.register-btn {
  width: 100%;
  height: 96rpx;
  line-height: 96rpx;
  font-size: 32rpx;
  color: #ffffff;
  background-color: #1989fa;
  border-radius: 48rpx;
  border: none;
}
</style>